package ydzhao.weixin.tuisong.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ydzhao.weixin.tuisong.entity.MessageType;
import ydzhao.weixin.tuisong.job.ThreadWorker;
import ydzhao.weixin.tuisong.service.WechatService;
import ydzhao.weixin.tuisong.util.WebChatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author chlong
 * @date 2022/8/9
 */
@RestController
@RequestMapping("/wechat")
public class WechatController {

    @Autowired
    private WechatService wechatService;

    // 开发者接入验证 确认请求来自微信服务器
    @RequestMapping(method = RequestMethod.GET)
    public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //消息来源可靠性验证
        String signature = request.getParameter("signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");       // 随机数
        String echostr = request.getParameter("echostr");//成为开发者验证
        //确认此次GET请求来自微信服务器，原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败
        PrintWriter out = response.getWriter();
        if(WebChatUtil.checkSignature(signature, timestamp, nonce)){
            System.out.println("=======请求校验成功======" + echostr);
            out.print(echostr);
        }
        out.close();
        out = null;
    }

    @RequestMapping(method=RequestMethod.POST)
    public void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO 消息的接收、处理、响应
        //消息来源可靠性验证
        String signature = request.getParameter("signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");       // 随机数
        //确认此次POST请求来自微信服务器，原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败
        if (!WebChatUtil.checkSignature(signature, timestamp, nonce)) {
            //消息不可靠，直接返回
            response.getWriter().write("");
            return;
        }
        //用户每次向公众号发送消息、或者产生自定义菜单点击事件时，响应URL将得到推送
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/xml");
            //调用parseXml方法解析请求消息
            Map<String, String> map = wechatService.parseXml(request, response);
            if (map.get("Content").contains("更新") || map.get("Content").contains("gengxin")){
                ThreadWorker threadWorker = new ThreadWorker();
                threadWorker.getThread(map.get("FromUserName"));
            }else {
                String MsgType = map.get("MsgType");
                String xml = null;//处理输入消息，返回结果的xml
                if(!MessageType.REQ_MESSAGE_TYPE_EVENT.equals(MsgType)){
    //                xml = wechatService.parseEvent(map);
    //            }else{
                    xml = wechatService.parseMessage(map);
                }
                //返回封装的xml
                //System.out.println(xml);
                response.getWriter().write(xml == null ? "" : xml);
            }
        } catch (Exception ex) {
            response.getWriter().write("");
        }
    }
}
